package bookshop.controller.backend;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bookshop.domain.Customer;
import bookshop.service.CustomerService;
import bookshop.service.helper.CustomerQueryHelper;
import bookshop.service.impl.CustomerServiceImpl;
import bookshop.utils.Page;

public class ManageCustomerServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		
		String act = request.getParameter("act");
		
//		if("loadall".equals(act)){
//			String customerId =request.getParameter("customerid");
//			CustomerService customerService =new CustomerServiceImpl();
//			List<Customer> customers =new ArrayList<Customer>();
//			
//			customers = customerService.loadall();
//			request.setAttribute("customers", customers);
//			request.getRequestDispatcher("jsps/backend/customer/list_customer.jsp").forward(request, response);
//		}
//		
		 if("qryloadall".equals(act)){
			//设置一个查询参数存储容器
			CustomerQueryHelper helper =new CustomerQueryHelper();
			
			//根据情况，把提交过来的查询参数写入helper,以方便传给后台，进行记录的条件过滤。
			if(request.getParameter("qryCustomerId")!=null && request.getParameter("qryCustomerId").length()!=0)
				helper.setQryCustomerId(request.getParameter("qryCustomerId"));
			
			if(request.getParameter("qryCustomerPwd")!=null && request.getParameter("qryCustomerPwd").length()!=0)
				helper.setQryCustomerPwd(request.getParameter("qryCustomerPwd"));
			
			if(request.getParameter("qryCustomerName")!=null && request.getParameter("qryCustomerName").length()!=0)
				helper.setQryCustomerName(request.getParameter("qryCustomerName"));
			
			if(request.getParameter("qryMoodLevel")!=null && request.getParameter("qryMoodLevel").length()!=0)
				helper.setQryMoodLevel(request.getParameter("qryMoodLevel"));
			
			if(request.getParameter("qryCustomerStatus")!=null && request.getParameter("qryCustomerStatus").length()!=0)
				helper.setQryCustomerStatus(request.getParameter("qryCustomerStatus"));
			
			//创建页面对象
			Page page =new Page();
			if(request.getParameter("pageNo")!=null)
				page.setPageNo(Integer.parseInt(request.getParameter("pageNo")));
			
			CustomerService customerService =new CustomerServiceImpl();
			
			request.setAttribute("page",customerService.loadCustomerByCondition(helper, page) );
			request.getRequestDispatcher("jsps/backend/customer/list_customer.jsp").forward(request, response);
		}
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doGet(request, response);
	}

}
